package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.AdminDto;
import com.woniu.model.dto.CoachDto;
import com.woniu.model.dto.VenueDto;
import com.woniu.model.entity.YAdmin;
import com.woniu.model.entity.YCoach;
import com.woniu.model.entity.YVenue;
import com.woniu.model.form.*;
import com.woniu.model.param.*;
import com.woniu.service.YAdminService;
import com.woniu.util.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 胡歌
 * @since 2024年05月22日
 */
@RestController
@RequestMapping("/yAdmin")
@Api(tags = "管理员操作信息的一些接口")
public class YAdminController {

    @Resource
    private YAdminService adminService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private OSSUtil ossUtil;
    /**
     * 发送注册验证码给管理员
     * @param
     * @return
     */
    @GetMapping("/sendAdminRegisterCode")
    @ApiOperation(value = "发送注册验证码给管理员")
    public Result sendCoachRegisterCode(@RequestParam("mobile") String mobile){
        adminService.sendRegisterCode(mobile);
        return Result.newInstance(null).setMsg("发送注册验证码成功").setCode(200);
    }

    /**
     * 管理员注册
     * @param
     * @return
     */
    @ApiOperation(value = "管理员注册的接口")
    @PostMapping("/checkCode")
    public Result checkCode(@RequestBody CodeAdminRegisterForm form)throws Exception{
        adminService.CodeAdminRegister(BeanUtil.toBean(form, CodeAdminRegisterParam.class));
        return Result.newInstance(null).setCode(200).setMsg("注册成功");
    }


    /**
     * 管理员发送登录短信
     * @param
     * @return
     */
    @ApiOperation(value = "管理员发送登录短信")
    @GetMapping("/LoginCodePhone")//发送登录短信
    public Result LoginCodePhone(@RequestParam("mobile") String mobile)throws Exception{
        adminService.LoginCodePhone(mobile);
        return Result.getResult().setMsg("发送登录短信成功").setCode(200);
    }

    /**
     * 管理员手机号登录
     * @param
     * @return
     * 返回整个管理员对象
     */
    @ApiOperation(value = "管理员手机号登录")
    @PostMapping("/codeLogin")
    public Result codeLogin(@RequestBody AdminCodeLoginForm form, HttpServletResponse response)throws Exception{
        AdminDto dto = adminService.codeLogin(BeanUtil.toBean(form, AdminCodeLoginParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(dto);
        //生成短的token
        HashMap<String, Object> accessBody = new HashMap<>();
        //使用雪花算法生成随机的不同的短token，确保不重复
        String random = new Snowflake(1, 1).nextIdStr();
        accessBody.put("random",random);
        //生成短token
        String accessToken = JwtUtil.createToken(accessBody, 15);
        //生成长token
        String refreshToken = JwtUtil.createToken(map, 3*24*60);
        //将长的token保存到redis中
        redisUtil.set(accessToken,refreshToken,3*24*60*60);//3天有效时间
        //相应token前端
        response.setHeader("token",accessToken);
        response.setHeader("Access-Control-Expose-Headers","token");
        return Result.newInstance(AdminDto.class).setData(dto).setMsg("登录成功");
    }

    /**
     *
     * @param form 管理员账号密码登录
     * @param resp 登录获得token
     * @return 返回整个管理员对象
     * @throws Exception 密码或者账号有误抛出异常
     */
    @ApiOperation(value = "管理员账号密码登录")
    @PostMapping("/adminLogin")
    public Result adminLogin(@RequestBody adminLoginForm form, HttpServletResponse resp)throws Exception{
        AdminDto dto = adminService.adminLogin(BeanUtil.toBean(form, adminLoginParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(dto);
        HashMap<String, Object> accessBody=new HashMap<String, Object>();
        String random=new Snowflake(1,1).nextIdStr();
        accessBody.put("random",random);
        String accessToken= JwtUtil.createToken(accessBody,15);
        String refreshToken=JwtUtil.createToken(map,3*24*60);
        redisUtil.set(accessToken,refreshToken,3*24*60*60);//3天有效时间
        resp.setHeader("token",accessToken);
        resp.setHeader("Access-Control-Expose-Headers","token");
        return Result.newInstance(AdminDto.class).setData(dto).setMsg("登录成功");
    }

    /**
     *
     * @param adminId 超管禁用管理员，传入需要禁用的ID
     * @return
     */
    @GetMapping("/disable")
    @ApiOperation("超管禁用管理员")
    public Result disable(Integer adminId){
        System.out.println("----"+adminId);
        adminService.disable(adminId);
        return Result.getResult().setMsg("禁用管理员成功").setCode(200);
    }

    /**
     *
     * @param adminId 超管启用管理员，传入需要启用的ID
     * @return
     */
    @GetMapping("/enable")
    @ApiOperation("超管启用管理员")
    public Result enable(Integer adminId){
        adminService.enable(adminId);
        return Result.getResult().setMsg("启用管理员成功").setCode(200);
    }

    /**
     * 根据id查找管理员
     * @param id
     * @return
     */
    @GetMapping("/byId/{id}")
    @ApiOperation("根据id查找管理员信息")
    public Result<AdminDto> byId(@PathVariable("id") Integer id){
        YAdmin admin = adminService.getById(id);
        return Result.newInstance(AdminDto.class).setData(BeanUtil.toBean(admin,AdminDto.class));
    }

    /**
     * 展示所有管理员
     * @return
     */
    @GetMapping("/seatAdminList")
    @ApiOperation("展示所有管理员")
    public Result<List<AdminDto>> seatInfoList(){
        List<YAdmin> infoList = adminService.list();
        List<AdminDto> list=new ArrayList<>();
        for (YAdmin info : infoList) {
            list.add(BeanUtil.toBean(info,AdminDto.class));
        }
        return Result.newListInstance(AdminDto.class).setData(list);
    }


    /**
     * 分页查询管理员
     *
     * @param form pageNum; pageSize;
     * @return
     * @throws YogaException
     */
    @GetMapping("/loadPageAdmin")
    @ApiOperation(value = "管理员分页查询接口")
    public Result loadPageAdmin( loadPageAllAdminForm form) throws YogaException {
        Page<YAdmin> page = adminService.loadPageAdmin(BeanUtil.toBean(form, loadPageAllAdminParam.class));
        return Result.newPageInstance(YAdmin.class).setMsg("查询成功").setCode(200).setData(page);
    }

    /**
     * 管理员注册电话号码验证
     *
     * @param phone 电话号码
     * @return
     * @throws YogaException
     */
    @GetMapping("/checkPhone")
    @ApiOperation(value = "管理员分页查询接口")
    public Result<?> checkPhone(Long phone) throws YogaException {
        adminService.checkPhone(phone);
        return Result.getResult().setMsg("该手机号可用").setCode(200);
    }
}

